package cn.iocoder.yudao.module.question.controller.admin.bank;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.question.controller.admin.bank.vo.*;
import cn.iocoder.yudao.module.question.dal.dataobject.bank.BankDO;
import cn.iocoder.yudao.module.question.convert.bank.BankConvert;
import cn.iocoder.yudao.module.question.service.bank.BankService;

@Tag(name = "管理后台 - 题库信息")
@RestController
@RequestMapping("/question/bank")
@Validated
public class BankController {

    @Resource
    private BankService bankService;

    @PostMapping("/create")
    @Operation(summary = "创建题库信息")
    @PreAuthorize("@ss.hasPermission('question:bank:create')")
    public CommonResult<Long> createBank(@Valid @RequestBody BankCreateReqVO createReqVO) {
        return success(bankService.createBank(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新题库信息")
    @PreAuthorize("@ss.hasPermission('question:bank:update')")
    public CommonResult<Boolean> updateBank(@Valid @RequestBody BankUpdateReqVO updateReqVO) {
        bankService.updateBank(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除题库信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('question:bank:delete')")
    public CommonResult<Boolean> deleteBank(@RequestParam("id") Long id) {
        bankService.deleteBank(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得题库信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('question:bank:query')")
    public CommonResult<BankRespVO> getBank(@RequestParam("id") Long id) {
        BankDO bank = bankService.getBank(id);
        return success(BankConvert.INSTANCE.convert(bank));
    }

    @GetMapping("/list")
    @Operation(summary = "获得题库信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('question:bank:query')")
    public CommonResult<List<BankRespVO>> getBankList(@RequestParam("ids") Collection<Long> ids) {
        List<BankDO> list = bankService.getBankList(ids);
        return success(BankConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/allList")
    @Operation(summary = "获得题库信息列表")
    @PreAuthorize("@ss.hasPermission('question:bank:query')")
    public CommonResult<List<BankRespVO>> getAllBankList() {
        List<BankDO> list = bankService.getBankList(new ArrayList<>());
        return success(BankConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得题库信息分页")
    @PreAuthorize("@ss.hasPermission('question:bank:query')")
    public CommonResult<PageResult<BankRespVO>> getBankPage(@Valid BankPageReqVO pageVO) {
        PageResult<BankDO> pageResult = bankService.getBankPage(pageVO);
        return success(BankConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出题库信息 Excel")
    @PreAuthorize("@ss.hasPermission('question:bank:export')")
    @OperateLog(type = EXPORT)
    public void exportBankExcel(@Valid BankExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BankDO> list = bankService.getBankList(exportReqVO);
        // 导出 Excel
        List<BankExcelVO> datas = BankConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "题库信息.xls", "数据", BankExcelVO.class, datas);
    }

}
